#include "vlpropertieswidget.h"

VLPropertiesWidget::VLPropertiesWidget(QWidget *parent) :
    QWidget(parent)
{
    numProps = 7;

    tableView = new QTableView();
    vertLayout = new QVBoxLayout();
    headers = new QStringList();
    *headers << "Property" << "Value";

    gameState = GameStates::getInstance();
    gameStateModel = new QStandardItemModel(6, 2);
    gameStateModel->setHorizontalHeaderLabels(*headers);
    pairList = new QList<propertyValuePair>();

    stepSizePr.value = new QStandardItem(QString::number(gameState->getStepSize()));
    stepSizePr.property = new QStandardItem(tr("Step size"));
    pairList->append(stepSizePr);

    jumpHeightPr.value = new QStandardItem(QString::number(gameState->getJumpHeight()));
    jumpHeightPr.property = new QStandardItem(tr("Jump height"));
    pairList->append(jumpHeightPr);

    gravityPr.value = new QStandardItem(QString::number(gameState->getGravity()));
    gravityPr.property = new QStandardItem(tr("Gravity"));
    pairList->append(gravityPr);

    restitutionPr.value = new QStandardItem(QString::number(gameState->getRestitution()));
    restitutionPr.property = new QStandardItem(tr("Restitution"));
    pairList->append(restitutionPr);

    viscousDragPr.value = new QStandardItem(QString::number(gameState->getViscousDrag()));
    viscousDragPr.property = new QStandardItem(tr("Viscous drag"));
    pairList->append(viscousDragPr);

    frictionPr.value = new QStandardItem(QString::number(gameState->getFriction()));
    frictionPr.property = new QStandardItem(tr("Friction"));
    pairList->append(frictionPr);

    hpChangeDelayPr.value = new QStandardItem(QString::number(gameState->getHpChangeDelay()));
    hpChangeDelayPr.property = new QStandardItem(tr("HP change delay"));
    pairList->append(hpChangeDelayPr);

    for (int row = 0; row < numProps; row++) {
            gameStateModel->setItem(row, 0, pairList->at(row).property);
            gameStateModel->setItem(row, 1, pairList->at(row).value);
            pairList->at(row).property->setEditable(false);
    }

    tableView->setModel(gameStateModel);
    tableView->verticalHeader()->hide();
    tableView->horizontalHeader()->setStretchLastSection(true);

    vertLayout->addWidget(new QLabel("Start Position"));

    posLayout= new QHBoxLayout();

    spX=new QSpinBox();
    spY=new QSpinBox();
    spZ=new QSpinBox();

    spX->setMinimum(0);
    spY->setMinimum(0);
    spZ->setMinimum(0);

    spX->setMaximum(WORLD_XSIZE-1);
    spY->setMaximum(WORLD_YSIZE-1);
    spZ->setMaximum(WORLD_ZSIZE-1);

    spX->setValue(gameState->getStartPosition().x);
    spY->setValue(gameState->getStartPosition().y);
    spZ->setValue(gameState->getStartPosition().z);

    connect(spX,SIGNAL(valueChanged(int)),this,SLOT(setStartPosition(int)));
    connect(spY,SIGNAL(valueChanged(int)),this,SLOT(setStartPosition(int)));
    connect(spZ,SIGNAL(valueChanged(int)),this,SLOT(setStartPosition(int)));

    posLayout->addWidget(new QLabel("x:"));
    posLayout->addWidget(spX);
    posLayout->addWidget(new QLabel("y:"));
    posLayout->addWidget(spY);
    posLayout->addWidget(new QLabel("z:"));
    posLayout->addWidget(spZ);

    vertLayout->addLayout(posLayout);

    resetButton = new QPushButton("Reset Game");
    connect(resetButton,SIGNAL(clicked()),this,SLOT(resetGame()));
    vertLayout->addWidget(resetButton);

    vertLayout->addWidget(tableView);

    doubleDelegate = new VLDoubleDelegate(tableView);
    tableView->setItemDelegateForColumn(1, doubleDelegate);

    connect(gameStateModel, SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(setModelData(QModelIndex, QModelIndex)));

    this->setLayout(vertLayout);
}

void VLPropertiesWidget::setModelData(QModelIndex topLeft, QModelIndex bottomRight) {
    QVariant val = topLeft.model()->data(topLeft, Qt::DisplayRole);
    float floatVal = val.toFloat();
    int intVal = val.toInt();

    switch (topLeft.row()) {
    case STEP_SIZE:
        gameState->setStepSize(floatVal);
        break;
    case JUMP_HEIGHT:
        gameState->setJumpHeight(floatVal);
        break;
    case GRAVITY:
        gameState->setGravity(floatVal);
        break;
    case RESTITUTION:
        gameState->setRestitution(floatVal);
        break;
    case VISCOUS_DRAG:
        gameState->setViscousDrag(floatVal);
        break;
    case FRICTION:
        gameState->setFriction(floatVal);
        break;
    case HP_CHANGE_DELAY:
        gameState->setHpChangeDelay(intVal);
        break;
    default:
        break;
    }
}

void VLPropertiesWidget::resetGame()
{
    GENG->resetGame();
}

void VLPropertiesWidget::setStartPosition(int n)
{
    GS->setStartPosition(vector3<int>(spX->value(),spY->value(),spZ->value()));
}

QSize VLPropertiesWidget::sizeHint() const {
    //tableView->resizeColumnsToContents();
    int width = tableView->columnWidth(0) + tableView->columnWidth(1) + 30;
    QSize *size = new QSize();
    size->setWidth(width);
    return *size;
}
